package org.zyx.item.service;

import org.springframework.stereotype.Service;
import org.zyx.item.entity.Category;
import org.zyx.item.mapper.CategoryMapper;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 根据父id查询节点
     * @param pid 父类别id
     * @return
     */
    public List<Category> findCategoryByPid(Long pid){
        Category recode = new Category();
        recode.setParentId(pid);
        return categoryMapper.select(recode);
    }

    public Long addCategory(Category category) {
        categoryMapper.insertSelective(category);//会自动将id设置到实体类中
        System.out.println(category);
        return  category.getId();
    }

    public Integer deleteCategoryById(Long id) {
        return categoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id list查询分类集合
     * @param ids
     * @return
     */
    public List<String> findByIds(List<Long> ids){
        List<Category> categories = categoryMapper.selectByIdList(ids);
        return categories.stream().map(category -> category.getName()).collect(Collectors.toList());
    }


}
